/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.modules.sys.dao.ShopAddDao;
import com.thinkgem.jeesite.modules.sys.dao.TbSenderDao;
import com.thinkgem.jeesite.modules.sys.entity.TbMpWallet;
import com.thinkgem.jeesite.modules.sys.entity.TbOrder;
import com.thinkgem.jeesite.modules.sys.entity.TbSender;
import com.thinkgem.jeesite.modules.sys.entity.TbSenderOrder;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 配送员管理Service
 * @author huoyan403
 * @version 2016-11-09
 */
@Service
@Transactional(readOnly = true)
public class TbSenderService {
	@Autowired
	TbSenderDao tbSenderDao;
	@Autowired
	ShopAddDao shopAddDao;
	
	public TbSender get(String id) {
		TbSender tbSender = tbSenderDao.get(id);
		return tbSender;
	}
	public TbSender checkSenderByMobile(String mobile) {
		return tbSenderDao.checkSenderByMobile(mobile);
	}
	
	public List<TbSender> findList(TbSender tbSender) {
		return tbSenderDao.findList(tbSender);
	}
	
	public Page<TbSender> findPage(Page<TbSender> page, TbSender tbSender) {
		tbSender.setPage(page);
		page.setList(tbSenderDao.findList(tbSender));
		return page;
	}
	
	@Transactional(readOnly = false)
	public void  save(TbSender tbSender) {
		Date date = new Date();
		tbSender.setCreateTime(date);
		tbSender.setUpdateTime(date);
		if(tbSender.getId()!=null&&!tbSender.getId().equals("")){
			tbSenderDao.update(tbSender);
		}else{
			//tbSender.setId("psy"+tbSender.getId());
			tbSender.setId("psy"+ IdGen.uuid());
			tbSenderDao.insert(tbSender);
			//根据时间查询派送员信息
			//TbSender tbsend=tbSenderDao.checkSenderByMobile(tbSender.getMobile());
			//派送员添加成功后维护麻雀钱包
			//代理商添加成功后，初始化麻雀钱包账户
			TbMpWallet tbMpWallet=new TbMpWallet();
			tbMpWallet.setUid(tbSender.getId());
			tbMpWallet.setCreateTime(new Date());
			tbMpWallet.setUpdateTime(new Date());
			//派送员的类型是3
			tbMpWallet.setUserType(3);
			Integer rows=shopAddDao.insertMpWallet(tbMpWallet);
		}

	}
	
	@Transactional(readOnly = false)
	public void delete(TbSender tbSender) {
		tbSenderDao.delete(tbSender);
	}

	public Integer findShopidByUid(String uid){
		return tbSenderDao.findShopidByUid(uid);
	}

    public List<TbSender> selectRegistrationIdbyUserId(String userId) {
		return  tbSenderDao.selectRegistrationIdbyUserId(userId);
    }
	@Transactional(readOnly = false)
    public  Integer insertSenderOrder(TbSenderOrder senderOorder) {
		senderOorder.setUid("0");
		senderOorder.setId(UUID.randomUUID()+"");
		senderOorder.setOrderStatus(0);
		senderOorder.setStandbyMobile("0");
		//根据uid的到shopid
		Integer shopid=tbSenderDao.findShopidByUid(UserUtils.getUser().getId());
		senderOorder.setShopId(shopid);
		return tbSenderDao.insertSenderOrder(senderOorder);
    }
	@Transactional(readOnly = false)
	public Integer insertStream(TbOrder tbOrder){
		return tbSenderDao.insertStream(tbOrder);
	}
}